package persistence.javaserialize;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import model.entity.Product;
import model.entity.container.ProductContainer;
import model.exceptions.InvalidNameException;
import persistence.ProductsDAO;

import common.Persistence;
import common.Result;
import common.ValueResult;

/**
 * @author tdixon
 * 
 *         Contains methods for retrieving, adding, updating, and deleting products from serialized
 *         java objects.
 */
public class SerialProductsDAO implements ProductsDAO
{

  @SuppressWarnings("unchecked")
  @Override
  public ValueResult<Set<Product>> getAll()
  {
    try
    {
      return new ValueResult<Set<Product>>(true, "", (Set<Product>) Persistence.load("Products"));
    }
    catch (FileNotFoundException e)
    {
      return new ValueResult<Set<Product>>(true, "", new HashSet<Product>());
    }
    catch (ClassNotFoundException | InvalidNameException | IOException e)
    {
      return new ValueResult<Set<Product>>(false, e.getMessage());
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public ValueResult<Map<ProductContainer, Set<Product>>> getProductsByContainer()
  {
    // productsByContainer = (Map<ProductContainer, Set<Product>>) Persistence
    // .load("ProductsByContainer");
    try
    {
      return new ValueResult<Map<ProductContainer, Set<Product>>>(true, "",
          (Map<ProductContainer, Set<Product>>) Persistence.load("ProductsByContainer"));
    }
    catch (FileNotFoundException e)
    {
      return new ValueResult<Map<ProductContainer, Set<Product>>>(true, "",
          new HashMap<ProductContainer, Set<Product>>());
    }
    catch (Exception e)
    {
      return new ValueResult<Map<ProductContainer, Set<Product>>>(false, e.getMessage());
    }
  }

  @Override
  public Result add(Product product)
  {
    return new Result(false, "Not supported for this persistence type; use saveProducts instead.");
  }

  @Override
  public Result update(Product product)
  {
    return new Result(false, "Not supported for this persistence type; use saveProducts instead.");
  }

  @Override
  public Result delete(Product product)
  {
    return new Result(false, "Not supported for this persistence type; use saveProducts instead.");
  }

  @Override
  public Result saveProducts(java.util.Set<Product> products)
  {
    try
    {
      Persistence.save(products, "Products");
      return new Result(true);
    }
    catch (Exception e)
    {
      // e.printStackTrace();
      return new Result(false, e.getMessage());
    }
  }

  @Override
  public Result saveProductsByContainer(Map<ProductContainer, Set<Product>> productsByContainer)
  {
    try
    {
      Persistence.save(productsByContainer, "ProductsByContainer");
      return new Result(true);
    }
    catch (Exception e)
    {
      // e.printStackTrace();
      return new Result(false, e.getMessage());
    }
  }

@Override
public Result addToProductContainer(Product product, ProductContainer container) {
	System.out.println("serialize mode doesn't support this method");
	return null;
}
}
